﻿using BO;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace StoreWeb.system.order
{
    public partial class add : System.Web.UI.Page
    {
        Maticsoft.BLL.InventoryClass icbll = new Maticsoft.BLL.InventoryClass();
        Maticsoft.BLL.Inventory mlbll = new Maticsoft.BLL.Inventory();
        Maticsoft.BLL.Orders bll = new Maticsoft.BLL.Orders();
        Maticsoft.BLL.Dictionary dtbll = new Maticsoft.BLL.Dictionary();
        BLL.member mbbll = new BLL.member();
        BLL.DealersManage dlmbll = new BLL.DealersManage();
        Maticsoft.BLL.membersize mbsizebll = new Maticsoft.BLL.membersize();
        Maticsoft.BLL.Ordersize ordersizebll = new Maticsoft.BLL.Ordersize();
        Maticsoft.BLL.Ordertetisize ordertetisizebll = new Maticsoft.BLL.Ordertetisize();
        Maticsoft.BLL.ordervaluetype ordervaluetypebll = new Maticsoft.BLL.ordervaluetype();
        Maticsoft.BLL.OrderPartsStyle orderpartsstylebll = new Maticsoft.BLL.OrderPartsStyle();
        Maticsoft222.BLL.sqlexec sqlbll = new Maticsoft222.BLL.sqlexec();
        protected Model.DealersManage dm = new Model.DealersManage();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                HttpCookie cookie = Request.Cookies["UserLogin"];
                dm = dlmbll.GetModel(Convert.ToInt32(cookie.Values["dmid"]));
                //商品分类
                this.rpttype.DataSource = icbll.GetList("(cInvCCode like '05%') and iInvCGrade=2");
                this.rpttype.DataBind();
                //面料信息
                //this.rptml.DataSource = icbll.GetList("cInvCCode like '01%' and iInvCGrade<>1 order by cInvCCode asc");
                //this.rptml.DataBind();
                //快递信息
                this.ddlistkuaidi.DataSource = dtbll.GetList("parentcode='ORDER_KD'");
                this.ddlistkuaidi.DataTextField = "name";
                this.ddlistkuaidi.DataValueField = "code";
                this.ddlistkuaidi.DataBind();
                //来源信息
                this.ddlistlaiyuan.DataSource = dtbll.GetList("parentcode='ORDER_LY'");
                this.ddlistlaiyuan.DataTextField = "name";
                this.ddlistlaiyuan.DataValueField = "code";
                this.ddlistlaiyuan.DataBind();
                //初始化订单时间
                txtcreatetime.Value = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                txtendtime.Value = DateTime.Now.AddDays(14).ToString("yyyy-MM-dd");
                //客户体型
                this.cklisttixing.DataSource = dtbll.GetList("parentcode='ORDER_TX'");
                this.cklisttixing.DataTextField = "name";
                this.cklisttixing.DataValueField = "code";
                this.cklisttixing.DataBind();
                //生成订单编号
                hidordercode.Value = DateTime.Now.ToString("yyMMddHHmmss");
            }
        }
        protected string getmljson()
        {
            return JsonConvert.SerializeObject(sqlbll.query("select a.cinvcname as name,b.cInvStd as cinvstd,a.price,b.cinvcode as code from dbo.InventoryClass a left join Inventory b on a.cInvCCode=b.cInvCCode where a.cInvCCode like '01%' and a.iInvCGrade<>1 order by a.cInvCCode asc").Tables[0]);
        }
        //保存订单信息
        protected void btnsubmit_Click(object sender, EventArgs e)
        {
            HttpCookie cookie = Request.Cookies["UserLogin"];
            //判断余额是否足够
            //Model.DealersManage dm = dlmbll.GetModel(Convert.ToInt32(cookie.Values["dmid"]));
            //if (dm != null && dm.moneybalance != null)
            //{
            //    if (dm.moneybalance < Convert.ToDecimal(hidorderprice.Value))
            //        Page.ClientScript.RegisterStartupScript(Page.GetType(), "", " <script>parent.layer.msg(\"门店余额不足！\"); </script>");
            //    else
            //    {
            //        dm.moneybalance -= Convert.ToDecimal(hidorderprice.Value);
            //        dlmbll.Update(dm);//扣除门店余额
            //    }
            //}
            //保存会员信息
            List<Model.member> mblist = mbbll.GetModelList("mobile='" + txtphone.Value + "'");
            Model.member md = new Model.member();
            if (mblist.Count > 0)
            {
                md = mblist[0];
            }
            md.address = txtaddress.Value;
            md.birthday = txtbothday.Value;
            md.constellation = txtconstellation.Value;
            md.emal = txtemail.Value;
            md.height = txtheight.Value;
            md.isvip = "1";
            md.mobile = txtphone.Value;
            md.name = txtname.Value;
            md.dealersid = cookie.Values["dmid"];
            string[] proSub = Request.Form.GetValues("s_province");
            string pro1 = proSub[s_province.SelectedIndex];

            string[] proSub2 = Request.Form.GetValues("s_city");
            string pro2 = proSub2[s_city.SelectedIndex];

            string[] proSub3 = Request.Form.GetValues("s_county");
            string pro3 = proSub3[s_county.SelectedIndex];
            md.s_city = pro2;
            md.s_county = pro3;
            md.s_province = pro1;
            md.sex = Convert.ToInt32(ddlistsex.SelectedItem.Value);
            md.type = 1;
            md.weight = txtweight.Value;
            //存在会员则修改
            if (mblist.Count > 0)
            {
                mbbll.Update(md);
            }
            //不存在会员添加
            else
            {
                md.createtime = DateTime.Now;
                md.code = DateTime.Now.ToString("yyyyMMddHHmmss");
                mbbll.Add(md);
                md.id = mbbll.GetModelList("mobile='" + txtphone.Value + "'").First().id;
            }
            //添加订单信息
            Maticsoft.Model.Orders order = new Maticsoft.Model.Orders();
            order.DealersManageID = cookie.Values["dmid"];
            order.address = txtaddress.Value;
            order.attribute = Convert.ToInt32(rdobtntypelist.SelectedItem.Value);
            order.cInvCCode = hidcInvCCode.Value;
            order.cInvCName = hidcInvCName.Value;
            order.cInvCode = hidInventorycode.Value;
            order.cInvnote = hidInventoryname.Value;
            order.code = hidordercode.Value;
            order.createtime = DateTime.Now;
            order.crname = ddlistkuaidi.SelectedItem.Text;
            order.crvalue = ddlistkuaidi.SelectedItem.Value;
            order.label = ddlistbiaoqian.SelectedItem.Text;
            order.lableextension = biaoqian.Value;
            order.membercode = md.id.ToString();
            order.size = hidsizecode.Value;
            order.membermobile = txtphone.Value;
            order.backupprice = Convert.ToDecimal(hidorderbackupprice.Value);
            order.membername = txtname.Value;
            order.cinvstd = hidcinvstd.Value;
            order.orderpaystatus = 2;
            order.orderremove = 1;
            order.cinvstdbackup = hidcinvstd2.Value;
            order.sizebackup = hidsizecode2.Value;
            order.cInvprice = Convert.ToDecimal(hidInventoryprice.Value);
            order.orderstatus = 8;
            order.originalordercode = originalordercode.Value;
            order.paystatus = Convert.ToInt32(rdobtnpaytypelist.SelectedItem.Value);
            order.price = Convert.ToDecimal(hidorderprice.Value);
            order.quyitime = Convert.ToDateTime(txtendtime.Value);
            order.s_city = pro2;
            order.s_county = pro3;
            order.s_province = pro1;
            order.shiyitime = Convert.ToDateTime(txtendtime.Value);
            order.istz = hidistz.Value;
            order.cInvCNames = hidcInvCNames.Value;
            order.source = ddlistlaiyuan.SelectedItem.Text;
            order.styleCode = "";
            order.synchronous = 0;
            order.imgs = hidurls.Value;
            order.note = txtnote.Value;
            List<Maticsoft.Model.OrderPartsStyle> orderpartsstylemodel = JsonConvert.DeserializeObject<List<Maticsoft.Model.OrderPartsStyle>>(hidstyle.Value);
            List<Maticsoft.Model.Ordersize> ordersizemodel = JsonConvert.DeserializeObject<List<Maticsoft.Model.Ordersize>>(hidsize.Value);
            List<Maticsoft.Model.Ordertetisize> ordertetisizemodel = JsonConvert.DeserializeObject<List<Maticsoft.Model.Ordertetisize>>(hidtetisize.Value);
            List<Maticsoft.Model.ordervaluetype> ordervaluetypemodel = JsonConvert.DeserializeObject<List<Maticsoft.Model.ordervaluetype>>(hidvaluetype.Value);
            //List<Maticsoft.Model.membersize> mlist = mbsizebll.GetModelList("sizecode='" + hidsizecode.Value + "' and membercode='" + txtphone.Value + "'");
            if (ordersizemodel != null && ordersizemodel.Count > 0)
            {
                foreach (var item in ordersizemodel)
                {
                    ordersizebll.Add(item);
                    ////保存客户尺寸
                    //Maticsoft.Model.membersize membersize = new Maticsoft.Model.membersize();
                    //membersize.sizecode = item.sizecode;
                    //membersize.name = item.sizename;
                    //membersize.sizeid = Convert.ToInt32(item.sizeid);
                    //membersize.value = item.sizevalue;
                    //if (mlist.Any(c => c.sizeid == Convert.ToInt32(item.sizeid)))
                    //{
                    //    membersize.id = mlist.First(c => c.sizeid == Convert.ToInt32(item.sizeid)).id;
                    //    mbsizebll.Update(membersize);
                    //}
                    //else
                    //{
                    //    mbsizebll.Add(membersize);
                    //}
                }
            }
            if (ordertetisizemodel != null && ordertetisizemodel.Count > 0)
                foreach (var item in ordertetisizemodel)
                {
                    ordertetisizebll.Add(item);
                }
            if (ordervaluetypemodel != null && ordervaluetypemodel.Count > 0)
                foreach (var item in ordervaluetypemodel)
                {
                    ordervaluetypebll.Add(item);
                }
            if (orderpartsstylemodel != null && orderpartsstylemodel.Count > 0)
                foreach (var item in orderpartsstylemodel)
                {
                    orderpartsstylebll.Add(item);
                }
            if (bll.Add(order) > 0)
                Page.ClientScript.RegisterStartupScript(Page.GetType(), "", " <script>parent.layer.alert(\"添加成功\", function (index) { parent.layer.close(index);layer_close();});</script>");
            else
                Page.ClientScript.RegisterStartupScript(Page.GetType(), "", " <script>parent.layer.msg(\"添加失败\"); </script>");
        }
    }
}